package com.example.management.controller;

import com.example.management.mapper.ManagerMapper;
import com.example.management.pojo.Manager;
import com.example.management.service.ManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

import static com.example.management.util.TokenUtil.*;

@Api(tags = "管理员")
@RestController
@RequestMapping("/manager")
public class ManagerController {

    @Autowired
    private ManagerMapper managerMapper;
    @Autowired
    private ManagerService managerService;

    @ApiOperation("所有管理员账户")
    @PostMapping("getAll")
    public List<Manager> getAll() {
        return managerService.getAllManager();
    }

    @ApiOperation("注册账户")
    @PostMapping("signUp")
    public String signUpManager(@RequestParam String account, @RequestParam String password, @RequestParam String name) {
        Manager manager = new Manager();
        manager.setAccount(account);
        manager.setPassword(password);
        manager.setName(name);
        return managerService.signUp(manager);
    }


    @ApiOperation("登录账户")
    @PostMapping("login")
    public String loginManager(@RequestParam String account, @RequestParam String password, HttpSession session) {
        Manager manager = new Manager();
        manager.setAccount(account);
        manager.setPassword(password);
        String token = createJwtToken(manager.getAccount(), ISSUER, manager.getName(), TTLMILLIS);
        if (!managerService.login(manager).equals("登录失败")) {

            managerMapper.addToken(manager, token);
            session.setAttribute("account", account);
        }
        return token;
    }

    @ApiOperation("修改账户信息")
    @PostMapping("change")
    public String changeManager(@RequestParam String account, @RequestParam String password
            , @RequestParam String newPassword, String newName) {
        Manager manager = new Manager();
        manager.setAccount(account);
        manager.setPassword(password);
        return managerService.changeMes(manager, newName, newPassword);
    }

    @ApiOperation("测试拦截机器")
    @PostMapping("loginT")
    public String testManager(@RequestParam("account") String account, @RequestParam("password") String password
            , HttpSession session) {
        Manager manager = new Manager();
        manager.setAccount(account);
        manager.setPassword(password);
        if (managerService.login(manager).equals("登录失败")) {
            return "用户名或密码错误";
        } else {
            session.setAttribute("account", account);
            return "访问成功";
        }
    }

/*    @ApiOperation("通过Token获取用户")
//    需要有Header，用到Postman？
    @PostMapping("getUser")
    public Result getUserOfLogin(HttpServletRequest request) throws UnknownError {
        String token = request.getHeader("token");
//        Object Manager=    根据tokne搜索用户
//        返回用户
        return null;
    }*/
    
}
//虽然过程有些滑稽，不过结果应该是成功了……
